<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        //$this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>[ 您现在访问的是Home模块的Index控制器 ]</div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
    	//dump($_SERVER);
     //    echo "<br>" . $_SERVER['SCRIPT_NAME'];
    	// echo "<br>" .U('Index/index');
     //    echo "<br>" . U('register');
    	$this->display(login);
    }
    public function register(){
        //dump($_SERVER);
        // echo "<br>" . $_SERVER['SCRIPT_NAME'];
        // echo "<br>" .U('Index/index');
        // echo "<br>" . U('register');
        $this->display(register);
    }    
    public function registerDo(){
        if(!IS_POST) $this->error('非法请求');

        if(!check_verify(I('post.verify'))){
            //echo('验证码输入错误！');
            echo "<script>alert('验证码输入错误！');history.back(-1);</script>";
            die;
        }
        if(I('post.upw1')==""){
            echo "<script>alert('password error');history.back(-1);</script>";
            die;
        }
        if(I('post.upw1')!=I('post.upw2')){
            echo "<script>alert('password error');history.back(-1);</script>";
            die;
        }
        $data = array(
            'uid'=>I('post.uid'),
            'upw'=>I('post.upw1'),
            'utel'=>I('post.utel'),
            'uname'=>I('post.uname'),
            'ulevel'=>0,
            'addip' => get_client_ip(),
            'addtime' => date("YmdHis")
            );
        $id = M('user_info')->data($data)->add();
        if($id>0){
            //echo "<script>alert('insert OK');</script>";
            $this->redirect('Index/index', 0, 1, 'OK');
            //index();
        }
        else{
            echo "<script>alert('insert Error');history.back(-1);</script>";
        }

    }    
    public function login(){
        if(!IS_POST) $this->error('非法请求');
        //p($_SESSION);
        //echo check_verify(I('post.verify'));

        if(!check_verify(I('post.verify'))){
            //echo('验证码输入错误！');
            echo "<script>alert('验证码输入错误！');history.back(-1);</script>";
            die;
        }
        else{
            //echo('验证码输入OK！');
        }
        $uid=I('post.uid');
        $upw=I('post.upw');
        $data = M('user_info')->where("uid='%s' and upw='%s'",$uid,$upw)->find();
        //$data = M('user_info')->where("uid='%s'",$uid)->select();
        //p( $data);
        if(!$data){
            //echo('验证码输入错误！');
            echo "<script>alert('id pw error');history.back(-1);</script>";
            die;
        }
        if($data['ulevel']<1){
            echo "<script>alert('未开通');history.back(-1);</script>";
            die;
        }
        $datain=array(
            'uid' =>$uid,
            'addtime' => date("YmdHis"),
            'addip' => get_client_ip()
            );
        //p($datain);
        //写日志
        $pid = M('user_longin_log')->data($datain)->add(); 

        session('uid',$uid);
        session('ulevel',$data['ulevel']);

        $this->redirect('Customer/index');//, 0, 1, '');
    }
    public function verify(){
        $config =    array(
            'fontSize'    =>    14,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
            'useCurve'    =>    false//是否使用混淆曲线 默认为true

        );
        $Verify =     new \Think\Verify($config);
        //$Verify =     new \Think\Verify();
        //ob_clean();
        $Verify->entry();
    }



}